﻿using System;
using System.Net.Mail;
using Headspring.Training.Domain.Model;
using Headspring.Training.Domain.Services;
using Headspring.Training.Domain.Services.Impl;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;

namespace Headspring.Training.WebUI.UnitTests.Services
{
    [TestFixture]
    public class EmailSenderTester
    {
        [Test]
        public void ShouldSendEmail()
        {
            MockRepository mocks = new MockRepository();
            ISmtpSender smtpSender = mocks.CreateMock<ISmtpSender>();
            smtpSender.Send(null);
            MailMessage actualMessage = null;

            LastCall.IgnoreArguments().Do(new Action<MailMessage>(delegate(MailMessage obj) { actualMessage = obj; }));
            mocks.ReplayAll();


            Employee recipient = new Employee("1", "Assignee", "Assignee", "recipient@test.com");
            Employee sender = new Employee("2", "Creator", "Creator", "sender@test.com");

            WorkOrder workOrder = new WorkOrder();
            workOrder.Assignee = recipient;
            workOrder.Creator = recipient;
            workOrder.CreatedDate = DateTime.Now;
            workOrder.Description = "foo";
            workOrder.Number = "12345";
            workOrder.Status = WorkOrderStatus.Assigned;
            workOrder.Title = "Foo 1";

            WorkOrderNotification notification = new WorkOrderNotification();
            notification.PreviousStatus = WorkOrderStatus.Draft;
            notification.WorkOrder = workOrder;
            notification.Recipient = recipient;
            notification.From = sender;

            EmailSender emailSender = new EmailSender(smtpSender);
            emailSender.Send(notification);

            mocks.VerifyAll();
            
            Assert.That(actualMessage.To, Is.EqualTo(new MailAddress(recipient.EmailAddress)));
            Assert.That(actualMessage.From, Is.EqualTo(new MailAddress(sender.EmailAddress)));
            Assert.IsNotEmpty(actualMessage.Subject);
        }
    }
}